//
//  SDP_PlayerViewController.m
//  ProjectB_
//
//  Created by sp on 16/3/28.
//  Copyright © 2016年 项目B. All rights reserved.
//
#import <AVFoundation/AVFoundation.h>

#import "SDP_PlayerViewController.h"    //C

#import "SDP_ViewForAVPlayerWithUrl.h"  //V
#import "KSBarrageView.h"

#import "EveryDayModel.h"   //M

#define WIDTH  [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height


@interface SDP_PlayerViewController ()

@property (nonatomic, strong)SDP_ViewForAVPlayerWithUrl *viewForPlyer;

@property (nonatomic, strong) KSBarrageView *barrageView;

@property (nonatomic, strong) NSMutableArray *arrForBarrageData;

@property (nonatomic, strong) NSString *nextPageUrl;
@end

@implementation SDP_PlayerViewController


/** 移除通知中心需要在 dealloc 中移除 */

- (void)dealloc {
    [self.viewForPlyer.moviePlayerItem removeObserver:self.viewForPlyer forKeyPath:@"status"];
    [self.viewForPlyer.moviePlayerItem removeObserver:self.viewForPlyer forKeyPath:@"loadedTimeRanges"];
    [self.viewForPlyer.moviePlayerItem removeObserver:self.viewForPlyer forKeyPath:@"playbackBufferEmpty"];
    [self.viewForPlyer.moviePlayerItem removeObserver:self.viewForPlyer forKeyPath:@"playbackLikelyToKeepUp"];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self handleData];
    
    EveryDayModel *model = [self.modelArray objectAtIndex:self.indexForMovie];
    
    self.viewForPlyer = [SDP_ViewForAVPlayerWithUrl shareVideoPlayer];
    NSString *strForEverydaySelected = model.playUrl;
    NSString *strForMovieTitle = model.title;
    
    // 视频网址和标题赋值
    [self.viewForPlyer passPlayerItemOfUrl:strForEverydaySelected];
    [self.viewForPlyer passMovieTitle:strForMovieTitle];
    [self.view addSubview:self.viewForPlyer];
    
    [self.viewForPlyer.buttonForBack addTarget:self action:@selector(testAction) forControlEvents:UIControlEventTouchUpInside];

    [self.viewForPlyer.buttonForNext addTarget:self action:@selector(replaceItems:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    
}


#pragma mark -- 视频切换

- (void)replaceItems:(UIButton *)button{
    
    
    NSLog(@"切换下一视频");
    
    //切换视频
//        [self.viewForPlyer.moviePlayer replaceCurrentItemWithPlayerItem:self.viewForPlyer.moviePlayerItem];

}

/** 视频将要出现时判断播放按键状态, plsy 为 selected 状态,如果暂停切换/退出视频,那么取反,因为视频进入视频为播放状态,如果不取反,那么视频为播放状态,但是播放按键为暂停*/

- (void)viewWillAppear:(BOOL)animated{

    if ([self.viewForPlyer.buttonForPlay isSelected]) {
        self.viewForPlyer.buttonForPlay.selected = !self.viewForPlyer.buttonForPlay.selected;
    }
}



#pragma mark -- 点击返回,跳转到前一页

- (void)testAction {

    // 暂停视屏播放
    [self.viewForPlyer.moviePlayer pause];
    
    [self.viewForPlyer removeFromSuperview];
    [self.viewForPlyer.moviePlayer replaceCurrentItemWithPlayerItem:nil];
    
    
    /** 模态返回上一个界面*/
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    
    
    
    
}

/** 强转屏幕横屏*/

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    
    return UIInterfaceOrientationMaskLandscapeRight;

}
// 解析弹幕数据
- (void)handleData {
    NSLog(@"当前ID号: %@", [[self.modelArray objectAtIndex:self.indexForMovie] id]);
    self.arrForBarrageData = [NSMutableArray array];
    NSString *url = [NSString stringWithFormat:@"http://baobab.wandoujia.com/api/v1/replies/video?_s=2334d5780841e7b8049f358aba3ec298&f=iphone&id=%@&net=wifi&num=20&u=ddfa7c45b86689f0e8cdcdf2387ceab5b7abbedc&v=1.12.1&vc=403", [[self.modelArray objectAtIndex:self.indexForMovie] id]];
    [NetWorkHandler networkingWithUrl:url comleteHandle:^(id result, NSData *data) {
       
        self.nextPageUrl = [result objectForKey:@"nextPageUrl"];
        
        NSArray *arr = [result objectForKey:@"replyList"];
        for (NSDictionary *dic in arr) {
            NSString *text = [dic objectForKey:@"message"];
            NSDictionary *user = [dic objectForKey:@"user"];
            NSString *avatar = [user objectForKey:@"avatar"];
            NSDictionary *temp = [NSDictionary dictionaryWithObjects:@[avatar, text] forKeys:@[@"avatar", @"content"]];
            [self.arrForBarrageData addObject:temp];
        }
        
        [self.viewForPlyer setDataArray:self.arrForBarrageData];
        
        NSLog(@"评论个数:%ld", self.arrForBarrageData.count);

    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
